package router

import (
	"SimpleDockerPanel/config"
	"SimpleDockerPanel/controllers"
	"SimpleDockerPanel/pkg/logger"
	"github.com/gin-contrib/sessions"
	seesion_redis "github.com/gin-contrib/sessions/redis"
	"github.com/gin-gonic/gin"
)

func Router() *gin.Engine {
	r := gin.Default()

	r.Use(gin.LoggerWithConfig(logger.LoggerToFile()))
	r.Use(logger.Recover)

	session, err := seesion_redis.NewStore(10, "tcp", config.RedisDB, "qwe123", []byte("secret"))
	if err != nil {
		logger.Write(err.Error(), "redis")
	}
	r.Use(sessions.Sessions("mysession", session))
	user := r.Group("/user")
	{
		user.POST("/login", controllers.UserController{}.Login)
		user.POST("/register", controllers.UserController{}.Register)
		user.GET("/info", controllers.UserController{}.GetUserInfo)
	}

	test := r.Group("/test")
	{
		test.GET("/success", controllers.TestController{}.SuccessTest)
		test.GET("/error", controllers.TestController{}.ErrorTest)
		test.GET("/getId/:id", controllers.TestController{}.GetParamsTest)
		test.GET("/getUserInfo/:id", controllers.TestController{}.GetUserInfoTest)
		test.POST("/getName", controllers.TestController{}.GetNameTest)
		test.POST("/getNameJson", controllers.TestController{}.GetNameJsonTest)
	}

	return r
}
